package com.lakienko.igor.random.values.rest;

import com.lakienko.igor.random.values.rest.convert.RandomsConverters;
import com.lakienko.igor.random.values.rest.model.in.ValueRestIn;
import com.lakienko.igor.random.values.rest.model.out.ValueRestOut;
import com.lakienko.igor.random.values.service.RandomValuesService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@RequiredArgsConstructor
@RequestMapping("api/1/random/value")
public class ValueController implements RandomsConverters {

    private final RandomValuesService randomValuesService;

    @GetMapping("/by/{categoryId}")
    ValueRestOut getValueByCategory(@PathVariable UUID categoryId) {
        return convert(randomValuesService.getValueByCategoryId(categoryId));

    }

    @PostMapping("/add")
    ValueRestOut addNewValue(@RequestBody ValueRestIn valueRestIn) {
        return convert(randomValuesService.addNewValue(convert(valueRestIn)));
    }

    @PutMapping("/update/{valueId}")
    ValueRestOut updateValue(@RequestBody @Valid ValueRestIn valueRestIn,
                             @PathVariable UUID valueId) {
        return convert(randomValuesService.updateValue(convert(valueRestIn, valueId)));
    }

    @DeleteMapping("/delete/{valueId}")
    void deleteValue(@PathVariable UUID valueId) {
        randomValuesService.deleteValue(valueId);
    }

}
